package com.exigen.perflabstat.models;

import java.util.ArrayList;
import java.util.List;

import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;

import org.primefaces.event.CellEditEvent;

import com.exigen.perflabstat.services.TableService;
import com.exigen.perflabstat.tables.models.ProjectNameModel;

public class TableDTO {
	
	private String PROJECT_NAME;
	private String COMMENTS;
	
	
	private TableService tableService;
	
	private List<ProjectName> projectName;
	private ProjectNameModel projectNameModel;
	private ProjectName[] selectedProjectName;
	private List<ProjectName> newProjectNames = new ArrayList<ProjectName>();
	private List<ProjectName> editProjectNames = new ArrayList<ProjectName>();
	private List<ProjectName> deleteProjectNames = new ArrayList<ProjectName>();

	private boolean renderAddRow = false;
	
	
	//Commit to database
	public void commit() {
		for(ProjectName project: newProjectNames)
			tableService.newProjectName(project);
		
		for(ProjectName project: deleteProjectNames)
			tableService.deleteProjectName(project);
		
		for(ProjectName project: editProjectNames)
			tableService.editProjectName(project);
		
		exit();
		FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Database successfully updated"));
 	}
	
	
	//Rollback changes
	public void exit() {
		projectName = null;
		selectedProjectName = null;
		newProjectNames = new ArrayList<ProjectName>();
		editProjectNames = new ArrayList<ProjectName>();
		deleteProjectNames = new ArrayList<ProjectName>();
	}
	
	
	//Edit Cells
	public void onCellEdit(CellEditEvent event) {  
        Object oldValue = event.getOldValue();  
        Object newValue = event.getNewValue();  
          
        if(newValue != null && !newValue.equals(oldValue)) {  
            //FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Cell Changed", "Old: " + oldValue + ", New:" + newValue);  
            //FacesContext.getCurrentInstance().addMessage(null, msg); 
        	
        	editProjectNames.add(projectName.get(event.getRowIndex()));
        }  
    }  
	
	
	//Add row toggle
	public void render() {
		renderAddRow = !renderAddRow;
	}
	
	
	//Creates and adds new projectName to list
	public void newProjectName() {
		ProjectName projectName = new ProjectName();
		projectName.setPROJECT_NAME(PROJECT_NAME);
		projectName.setCOMMENTS(COMMENTS);
		
		//tableService.newProjectName(projectName);
		
		this.projectName.add(projectName);
		
		//projectNameModel = new ProjectNameModel(this.projectName);
		
		newProjectNames.add(projectName);
		
	}
	
	
	//Delete selected projectName from list
	public void deleteSelected() {
		//tableService.deleteSelected(selectedProjectName);
		
		for(ProjectName selected: selectedProjectName) {
			deleteProjectNames.add(selected);
			projectName.remove(selected);
		}
	}
	
	
    //Getters and Setters -----------------------------------------------
	
	
	public List<ProjectName> getProjectName() {
		
		return projectName;
	}

	public void setProjectName(List<ProjectName> projectName) {
		
		this.projectName = tableService.projectName();
		
		projectNameModel = new ProjectNameModel(this.projectName);
	}

	public TableService getTableService() {
		return tableService;
	}

	public void setTableService(TableService tableService) {
		this.tableService = tableService;
	}

	public ProjectName[] getSelectedProjectName() {
		return selectedProjectName;
	}

	public void setSelectedProjectName(ProjectName[] selectedProjectName) {
		this.selectedProjectName = selectedProjectName;
	}

	public ProjectNameModel getProjectNameModel() {
		
		if(projectName == null)
			setProjectName(tableService.projectName());
		
		projectNameModel = new ProjectNameModel(projectName);
		
		return projectNameModel;
	}

	public void setProjectNameModel(ProjectNameModel projectNameModel) {
		this.projectNameModel = projectNameModel;
	}

	public String getPROJECT_NAME() {
		return PROJECT_NAME;
	}

	public void setPROJECT_NAME(String pROJECT_NAME) {
		PROJECT_NAME = pROJECT_NAME;
	}

	public String getCOMMENTS() {
		return COMMENTS;
	}

	public void setCOMMENTS(String cOMMENTS) {
		COMMENTS = cOMMENTS;
	}

	public List<ProjectName> getNewProjectNames() {
		return newProjectNames;
	}

	public void setNewProjectNames(List<ProjectName> newProjectNames) {
		this.newProjectNames = newProjectNames;
	}

	public List<ProjectName> getEditProjectNames() {
		return editProjectNames;
	}

	public void setEditProjectNames(List<ProjectName> editProjectNames) {
		this.editProjectNames = editProjectNames;
	}

	public List<ProjectName> getDeleteProjectNames() {
		return deleteProjectNames;
	}

	public void setDeleteProjectNames(List<ProjectName> deleteProjectNames) {
		this.deleteProjectNames = deleteProjectNames;
	}
	
	public boolean isRenderAddRow() {
		return renderAddRow;
	}

	public void setRenderAddRow(boolean renderAddRow) {
		this.renderAddRow = renderAddRow;
	}
	
}
